<!-- 余额管理 -->
<template>
  <div class="pa-20">
    <!-- <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="结算时间" prop="accType">
        <el-select v-model="queryParams.accType" placeholder="请选择查询类型">
            <el-option
              v-for="dict in dict.type.wtype"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value">
            </el-option>
          </el-select>
      </el-form-item>
    </el-form> -->
    <div class="disFlxAC pb-10">
      <div class="color6 mr-10 fz-16">
        当前账户可用余额<span class="px-20 fw-6 colorZS">{{ balanceObj.balance || 0 }}</span
        >元
        <span class="pl-10" v-if="balanceObj.sumBalance">(平台沉淀资金: <span class="colorZS fw-6">{{balanceObj.sumBalance}}</span> 元)</span>
      </div>
      <template v-if="balanceObj.userType !== 5">
        <el-button
          type="text"
          icon="el-icon-edit"
          @click="withdrawApplication"
          v-hasPermi="['finance:balance:application']"
          >提现申请</el-button
        >
        <el-button
          type="text"
          icon="el-icon-connection"
          @click="$router.push(`/finance/withdrawal`)"
          v-hasPermi="['finance:withdrawal:list']"
          >提现记录</el-button
        >
      </template>
    </div>
    <el-table v-loading="loading" :data="balanceList">
      <el-table-column
        label="结算时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bill_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="变更前余额" align="center" prop="balance" />
      <el-table-column label="变更后余额" align="center" prop="newBalance" />
      <el-table-column label="变更金额" align="center">
        <template slot-scope="{row}">
          <span v-if="row.number === 0">{{ row.number }}</span>
          <span v-else :style="row.pm == 1 ? 'color:#67C23A;':'color:#F56C6C;'">{{row.pm == 1 ? '+' : '-'}}{{row.number}}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" min-width="300" />
      <!-- <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 提现对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="当前余额">
          <div>{{ balanceObj.balance || 0 }}元</div>
        </el-form-item>
        <el-form-item label="提现金额" prop="applyAmount">
          <el-input-number
            v-model="form.applyAmount"
            controls-position="right"
            :min="0"
            :max="balanceObj.balance"
            :precision="2"
            :controls="false"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="提现方式" prop="payMethod">
          <el-radio-group v-model="form.payMethod">
            <el-radio :label="2">银行卡</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="收款人姓名" prop="payeeName">
          <el-input v-model="form.payeeName" placeholder="请输入收款人姓名" />
        </el-form-item>
        <el-form-item label="银行卡号" prop="bankCard">
          <el-input v-model="form.bankCard" placeholder="请输入银行卡号" />
        </el-form-item>
        <el-form-item label="开户行" prop="bankInfo">
          <el-input v-model="form.bankInfo" placeholder="请输入开户行" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { userAmount, withdrawal } from "@/api/order/balance";
import { listInfo } from "@/api/order/balancelog";

export default {
  name: "Balance",
  dicts: ["wtype", "bill_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 余额日志表格数据
      balanceList: [],
      //   当前余额对象
      balanceObj: {
        userType: 5
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        userTpe: null,
        userName: null,
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      form: {},
      rules: {
        applyAmount: [
          { required: true, message: "请输入提现金额", trigger: "blur" },
        ],
        payMethod: [
          { required: true, message: "请选择提现方式", trigger: "change" },
        ],
        payeeName: [
          { required: true, message: "请输入收款人姓名", trigger: "blur" },
        ],
        bankCard: [
          { required: true, message: "请输入银行卡号", trigger: "blur" },
        ],
        bankInfo: [
          { required: true, message: "请输入开户行", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getInfo();
  },
  activated() {
    this.getInfo();
  },
  computed: {
    userInfo() {
      return this.$store.state.user.userInfo;
    },
  },
  methods: {
    /** 查询余额变动记录 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.balanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    async getInfo() {
      const { data } = await userAmount();
      this.balanceObj = data;
      this.queryParams.userId = data.userId;
      this.queryParams.userTpe = data.userType;
      this.queryParams.userName = data.userName;
      this.getList();
    },
    reset() {
      this.form = {
        applyAmount: "",
        payMethod: 2,
        bankInfo: "",
        bankCard: "",
        payeeName: "",
      };
    },
    // 提现申请
    withdrawApplication() {
      if (this.balanceObj.balance) {
        this.reset();
        this.title = "申请提现";
        this.open = true;
      } else {
        this.$message.warning("暂无可提现余额");
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          withdrawal(this.form).then((res) => {
            this.$message.success("提现申请成功");
            this.reset();
            this.open = false;
          });
        }
      });
    },
  },
};
</script>

<style>
</style>
